import string
import killmailmodel
import playermodel

class KillmailParser(object):
    
    def __init__(self, Killmail):
        self.model = None
        self.victim = []
        self.involvedPlrs = []
        self.killmail = Killmail
        self.playermodel = None
        self.playermodelInUse = False
    
    def AddFile(self, Killmail):
        self.killmail = Killmail
    
    def PrintCurrent(self):
        print self.killmail

    def AddEmptyModel(self):
        self.model = killmailmodel.KillmailModel()
        
    def AddDate(self):
        print self.killmail[0]
        self.model.Date = self.killmail[0]
    
    def AddVictim(self):
        victim = []
        entryPoint = self.find("victim")
        count = 0
        while count <= 6:
            victim.append(self.killmail[entryPoint+count].split(": "))
            count += 1
        self.model.Victim = victim

    def AddInvolvedParties(self):
        involvedPlrs = []
        mainEntryPoint = self.find("involved parties")
        count = 0
        extraCount = 1
        toPlayer = False
        players = 0
        currPlayer = playermodel.PlayerModel()
        while count < len(self.killmail)-mainEntryPoint:
            if string.find(self.killmail[mainEntryPoint+count].lower(), "name:"):
                currPlayer.name = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                players += 1
                extraCount = 1
                toPlayer = True
            elif toPlayer == True:
                if count % players != 0:
                    if extraCount == 1:
                        currPlayer.securityStatus = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                    if extraCount == 2:
                        currPlayer.corporation = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                    if extraCount == 3:
                        currPlayer.alliance = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                    if extraCount == 4:
                        currPlayer.faction = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                    if extraCount == 5:
                        currPlayer.ship = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                    if extraCount == 6:
                        currPlayer.weapon = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                    if extraCount == 7:
                        currPlayer.damageDone = string.split(self.killmail[mainEntryPoint+count], ": ")[1]
                    extraCount += 1
                
                    
            count += 1
        self.involvedPlrs = involvedPlrs
        self.model.InvParties = involvedPlrs
    
    def find(self, keyword):
        entryPoint = 0
        count = 0
        for line in self.killmail:
            if string.find(line.lower(), keyword+":") > -1:
                entryPoint = count
            count += 1
        return entryPoint
    
    def AddName(self, name):
        self.playermodel.apped(name)
   
   
        